package logic

import (
	"context"
	"shop_srv/internal/svc"
	"shop_srv/models"
	"shop_srv/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListGoodsLogic {
	return &ListGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListGoodsLogic) ListGoods(in *shop.ListGoodsRequest) (*shop.ListGoodsResponse, error) {
	// todo: add your logic here and delete this line

	var g models.Goods
	var listGoods []*shop.ListGoods

	find, _ := g.GetGoods(int(in.Page), int(in.PageSize), in.Keyword, in.SortWord)

	for _, g := range find {
		list := shop.ListGoods{
			GoodsId: int64(g.ID),
			Title:   g.Title,
			Price:   float32(g.Price),
			Stock:   g.Stock,
			Status:  g.GoodsStatus,
		}
		listGoods = append(listGoods, &list)
	}
	return &shop.ListGoodsResponse{
		List: listGoods,
	}, nil
}
